package com.huxiu.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.huxiu.BaseActivity;
import com.huxiu.CommentActivity;
import com.huxiu.R;
import com.huxiu.SubmitHuatiCommentActivity;
import com.huxiu.net.GsonRequest;
import com.huxiu.net.NetworkConstants;
import com.huxiu.net.model.ArticleContent;
import com.huxiu.net.model.HuatiInfo;
import com.huxiu.net.model.User;
import com.huxiu.net.responses.BaseResponse;
import com.huxiu.net.responses.TuiJianDataResponse;
import com.huxiu.view.ArticleWebView;
import com.umeng.analytics.MobclickAgent;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by caoyuanjing on 15/12/3.
 */
public class JavaScriptinerface{
    public Context mContext;
    public ArticleContent mArticleContent;
    public static ArticleWebView webview;
    public HuatiInfo mdata;
    /**
     * Instantiate the interface and set the context
     */
    public JavaScriptinerface(Context c, ArticleContent mArticleContent, ArticleWebView webview) {
        this.mContext = c;
        this.mArticleContent = mArticleContent;
        this.webview = webview;
    }
    public JavaScriptinerface(Context c, HuatiInfo mdata, ArticleWebView webview) {
        this.mContext = c;
        this.mdata = mdata;
        this.webview = webview;
    }




    /****
     * 文章内容页面收藏
     */
    @JavascriptInterface
    public void collectionArticle() {

        if (!Settings.isLoggedIn()) {
            Utils.showToast("登录后才能收藏");
            Utils.askLogIn((Activity) mContext);
            return;
        } else {
            collectiondata();
        }
    }

    /****
     * 文章内容页面收藏请求数据
     */
    public  void collectiondata() {
        String url = NetworkConstants.ADD_FAVORITE_ARTICLE_URL;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("aid", mArticleContent.aid);
        GsonRequest<TuiJianDataResponse> request = new GsonRequest<>(url, Request.Method.POST,
                TuiJianDataResponse.class, false, params, new Response.Listener<TuiJianDataResponse>
                () {
            @Override
            public void onResponse(TuiJianDataResponse response) {
                Log.e("response", "response" + response.success + response.data);
                if (response.isSuccess()) {
//                    Utils.showToast("收藏成功");
                    webview.loadUrl("javascript:js_collection()");
                    UmenEvent.UmengEventMap(mContext, UmenEvent.APP_ARTICLE_CONTENT, UmenEvent.ARTICLE_DETAIL_COLLECT_ARTICLE);
                } else {
                    Utils.showToast(response.error.message);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Utils.showToast(error.getMessage());
            }
        });
        BaseActivity.getmQueue().add(request);
        //        mQueue.start();
    }


    /****
     * 文章内容页面跳转评论
     */
    @JavascriptInterface
    public void IntentComment() {
        UmenEvent.UmengEventMap(mContext, UmenEvent.APP_ARTICLE_CONTENT, UmenEvent.ARTICLE_DETAIL_ENTER_COMMENT_HOT);
        Intent intent = new Intent(mContext, CommentActivity.class);
        intent.putExtra(Constants.ARTICLE_ID_KEY, mArticleContent.aid);
        mContext.startActivity(intent);
    }


    /****
     * 文章内容页面点赞
     */
    @JavascriptInterface
    public void collectionprise() {
        if (!Settings.isLoggedIn()) {
            Utils.showToast("登录后才能点赞");
            Utils.askLogIn((Activity) mContext);
            return;
        } else {
            if(!mArticleContent.is_agree){
                parise();
            }else{
                Utils.showToast(mContext.getResources().getString(R.string.znaguo));
            }

        }
    }

    /****
     * 文章内容页面点赞请求数据
     */
    public  void parise() {
        String url = NetworkConstants.ZAN_URL;
        Map<String, String> params = new LinkedHashMap<>();
        if (mArticleContent.is_agree) {
            params.put("id", "-" + mArticleContent.aid);
            params.put("act", "agree_article");
        } else {
            params.put("id", mArticleContent.aid);
            params.put("act", "agree_article");
        }
        GsonRequest<TuiJianDataResponse> request = new GsonRequest<>(url, Request.Method.POST,
                TuiJianDataResponse.class, false, params, new Response.Listener<TuiJianDataResponse>() {
            @Override
            public void onResponse(TuiJianDataResponse response) {
                Log.e("response", "response" + response.success + response.data);
                if (response.isSuccess()) {
//                    Utils.showToast("操作成功");
                    Log.e("webviewui", "成功");
                    mArticleContent.is_agree = !mArticleContent.is_agree;
                    webview.loadUrl("javascript:js_praise()");
                    UmenEvent.UmengEventMap(mContext, UmenEvent.APP_ARTICLE_CONTENT, UmenEvent.ARTICLE_DETAIL_ARTICLE_LIKE);
                } else {
                    Log.e("webviewui", "失败");
                    Utils.showToast(response.error.message);
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Utils.showToast(error.getMessage());
            }
        });
        BaseActivity.getmQueue().add(request);
        //        mQueue.start();
    }

    /****
     * 文章内容页面初始化点赞和收藏
     */
    @JavascriptInterface
    public void webviewui() {
        if (mArticleContent.is_favorite) {
            webview.loadUrl("javascript:initialize_collection()");
        } else {
        }
        if (mArticleContent.is_agree) {
            webview.loadUrl("javascript:initialize_praise()");
        } else {
        }
    }

    /****
     * 话题文章内容页面点赞初始化
     */
    @JavascriptInterface
    public void thread_zan_ui(){
        if (mdata.thread.is_zan){
            Log.e("thread_zan_ui","thread_zdddan_ui");
            webview.loadUrl("javascript:initialize_praise()");
        } else {
            Log.e("thread_zan_ui","thread_zan_ui");
        }
    }

    /****
     * 话题文章内容页面点赞
     */
    @JavascriptInterface
    public void huatiprise() {
        if (!Settings.isLoggedIn()) {
            Utils.showToast("登录后才能点赞");
            Utils.askLogIn((Activity) mContext);
            return;
        } else {
            if(!mdata.thread.is_zan){
                thread_zan();
            }else{
                Utils.showToast(mContext.getResources().getString(R.string.znaguo));
            }


        }
    }

    /****
     * 话题文章内容页面点赞亲求数据
     */
    public void thread_zan() {
        String url = (mdata.thread.is_zan ? NetworkConstants.THREAD_UNZAN : NetworkConstants
                .THREAD_ZAN);
        Map<String, String> params = new LinkedHashMap<>();

        params.put("id", mdata.thread.id);

        GsonRequest<BaseResponse> request = new GsonRequest<>(url, Request.Method.POST,
                BaseResponse.class, false, params, new Response.Listener<BaseResponse>() {
            @Override
            public void onResponse(BaseResponse response) {
                if (response.isSuccess()) {
//                    Utils.showToast("操作成功");
                    webview.loadUrl("javascript:js_praise()");
                    mdata.thread.is_zan = !mdata.thread.is_zan;
                    UmenEvent.UmengEventMap(mContext, UmenEvent.APP_TOPIC_DETAIL, UmenEvent.TOPIC_DETAIL_LIKE);
                } else {
                    Log.e("webviewui", "失败");
                    Utils.showToast(response.error.message);
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Utils.showToast(error.getMessage());
            }
        });
        BaseActivity.getmQueue().add(request);
        //        mQueue.start();
    }


    /****
     * 话题评论回复弹出键盘
     */
    @JavascriptInterface
    public void huatiComment(String name){
        if (!(mdata.forum.is_join== 1)) {
            Utils.showToast("您还未加入该小组");
            return;
        }
        if (!Settings.isLoggedIn()){
            Utils.askLogIn((Activity)mContext);
            return;
        }
        Intent intent = new Intent(mContext, SubmitHuatiCommentActivity.class);
        intent.putExtra(Constants.HUATI_ID_KEY, mdata.thread.id);
        intent.putExtra("type", Constants.HUATICOMMENT_ADD);
        intent.putExtra(Constants.REPLYNAME,name);
        mContext.startActivity(intent);
    }
    /****
     * 话题评论回复点赞
     */
    @JavascriptInterface
    public void huatiPrise(String praiseid){
        if (!Settings.isLoggedIn()) {
            Utils.askLogIn((Activity)mContext);
            return;
        }
        Log.e("aaaa","praiseid"+praiseid);
        User mUser=Global.user;
        mUser.praiseid=praiseid;
        final String json=new Gson().toJson(mUser);
        Log.e("json", json);
        UmenEvent.UmengEventMap(mContext, UmenEvent.APP_TOPIC_DETAIL, UmenEvent.TOPIC_DETAIL_COMMENT_LIKE);

        webview.post(new Runnable() {
            @Override
            public void run() {webview.loadUrl("javascript:js_comment_praise(" + json.toString() + ")");
            }
        });
//        webview.loadUrl("javascript:js_comment_praise('" + json.toString() + "')");
    }

    public  static void huatiCommentH5(String content){

        User mUser=Global.user;
        mUser.content=content;
        final String json=new Gson().toJson(mUser);
        Log.e("json", json);
        webview.post(new Runnable() {
            @Override
            public void run() {
                webview.loadUrl("javascript:js_prepend_comment(" + json.toString() + ")");
            }
        });

    }
    /****
     * h5后台返回的评论结果输出
     */
    @JavascriptInterface
    public void ToastH5Reload(boolean status,String message){
        if(status){
            webview.reload();
            Utils.showToast(R.string.submit_comment_success);
        }else{
            Utils.showToast(message);
        }
    }

    /****
     * h5后台返回的评论结果输出
     */
    @JavascriptInterface
    public void ToastH5(String data){
        Log.e("json", data);
        BaseResponse mdata= new Gson().fromJson(data, BaseResponse.class);
        if(mdata.success){
//            Utils.showToast(R.string.submit_comment_success);
        }else{
            Utils.showToast(mdata.error.message);
        }
    }

    /****
     * h5后台返回的评论结果输出
     */
    @JavascriptInterface
    public void ToastH5NoReload(boolean status,String message){
        if(status){
//            Utils.showToast(R.string.submit_comment_success);
        }else{
            Utils.showToast(message);
        }
    }
}
